package com.richest.api.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author 赵鸿超 2017年9月25日
 */
public class CommonUtil {

  /**
   * 获取客户端真实Ip
   * 
   * @author 赵鸿超 2017年9月25日
   * @param request
   *          请求
   * @return String 客户端ip
   */
  public static String getIpAddr(HttpServletRequest request) {
    String strIp = null;
    String strIps = request.getHeader("x-forwarded-for");
    if (strIps != null && strIps.length() > 0) {
      String[] split = strIps.split(",");
      if (split.length > 0)
        strIp = split[0];
    }
    if (strIp == null || strIp.length() == 0 || "unknown".equalsIgnoreCase(strIp)) {
      strIp = request.getHeader("Proxy-Client-IP");
    }
    if (strIp == null || strIp.length() == 0 || "unknown".equalsIgnoreCase(strIp)) {
      strIp = request.getHeader("WL-Proxy-Client-IP");
    }
    if (strIp == null || strIp.length() == 0 || "unknown".equalsIgnoreCase(strIp)) {
      strIp = request.getRemoteAddr();
    }
    return strIp;
  }
}
